package dao

import (
	"context"
	"mini-app/internal/models"

	"gorm.io/gorm"
)

type OnlineEventSupportLog struct{}

func (OnlineEventSupportLog) Create(
	ctx context.Context,
	db *gorm.DB,
	openID string,
	supportID string,
) (success bool) {
	var count int64
	db.WithContext(ctx).Model(&models.OnlineEventSupportLog{}).
		Where("openid = ?", openID).
		Where("support_id = ?", supportID).
		Count(&count)
	if count > 0 {
		return false
	}
	rowsAffected := db.WithContext(ctx).Model(&models.OnlineEventSupportLog{}).Create(&models.OnlineEventSupportLog{
		OpenID:    openID,
		SupportID: supportID,
	}).RowsAffected
	return rowsAffected > 0
}

func (OnlineEventSupportLog) List(
	ctx context.Context,
	db *gorm.DB,
	openID string,
	supportID string,
) []models.OnlineEventSupportLog {
	var list []models.OnlineEventSupportLog
	tx := db.WithContext(ctx).Model(&models.OnlineEventSupportLog{})
	if openID != "" {
		tx = tx.Where("openid = ?", openID)
	}
	if supportID != "" {
		tx = tx.Where("support_id = ?", supportID)
	}
	tx.Find(&list)
	return list
}
